package com.arcvideo.acsconnect.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.arcvideo.acsconnect.rest.model.ElderFamily;
import com.arcvideo.acsconnect.rest.model.FamilyInfo;
import com.arcvideo.acsconnect.rest.model.UserBean;
import com.arcvideo.acsconnect.ui.LiveActivity;
import com.arcvideo.tyingyitonginhouse.R;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author shenmengchao
 * @version 1.0.0
 * @date 2018/7/12
 * @description
 */

public class UserSelectDialog extends Dialog {

    private final static String TAG = "UserSelectDialog";

    @BindView(R.id.list_view)
    RecyclerView mListView;
    @BindView(R.id.ll_root)
    LinearLayout mLlRoot;

    private UserAdapter mUserAdapter = new UserAdapter();
    private LinearLayoutManager mLinearLayoutManager;
    private LiveActivity mActivity;
    private List<UserBean> mList = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;

    public UserSelectDialog(@NonNull LiveActivity activity, List<UserBean> list) {
        super(activity, R.style.Dialog);
        this.mActivity = activity;
        this.mList = list;
        init();
        initView();
        initEvent();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setData(List<UserBean> list) {
        this.mList = list;
        mUserAdapter.setData(list);
    }

    public void updateData(String id, String onlineStatus) {
        if (null != mUserAdapter) {
            mUserAdapter.updateData(id, onlineStatus);
        }
    }

    private void init() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.layout_dialog_user_select, null, false);
        ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        ButterKnife.bind(this, view);
        addContentView(view, layoutParams);

        Window window = this.getWindow();
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = window.getAttributes();
        p.width = (int) (d.getWidth() * 0.2);
        p.height = (int) (d.getWidth() * 1);
        p.gravity = Gravity.RIGHT;
        window.setAttributes(p);
        window.setBackgroundDrawable(null);//去掉默认的边距
        window.setWindowAnimations(R.style.UserSelectDialogAnim);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_DOWN:
            case KeyEvent.KEYCODE_VOLUME_DOWN:
//                ToastUtil.showToast("down");
                break;

            case KeyEvent.KEYCODE_DPAD_UP:
//            case KeyEvent.KEYCODE_VOLUME_UP:
//                ToastUtil.showToast("up");
                break;

            case KeyEvent.KEYCODE_ENTER:     //确定键enter
            case KeyEvent.KEYCODE_DPAD_CENTER:
//                mListView.scrollBy(0, -500);
//                MoveToPosition(mLinearLayoutManager, mListView, 12);
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
            case KeyEvent.KEYCODE_DPAD_RIGHT:
            case KeyEvent.KEYCODE_BACK:
                dismiss();
                break;
            case KeyEvent.KEYCODE_VOLUME_UP:
//                mListView.scrollBy(0, 100);
                break;
            default:
                break;
        }

        return super.onKeyDown(keyCode, event);
    }

    private void initView() {

        LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams) mLlRoot.getLayoutParams();
        ll.width = mActivity.getWindowManager().getDefaultDisplay().getWidth() / 5;
        ll.height = mActivity.getWindowManager().getDefaultDisplay().getHeight();
        mLlRoot.setLayoutParams(ll);

        mUserAdapter.setData(mList);
        mUserAdapter.setHasStableIds(true);

        mListView.setAdapter(mUserAdapter);
        mListView.setItemAnimator(null);
        mLinearLayoutManager = new LinearLayoutManager(mActivity);
        mListView.setLayoutManager(mLinearLayoutManager);


    }

    private void initEvent() {
        mListView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
//                mListView.scrollBy(0, -100);
                Log.d(TAG, "onFocusChange");
            }
        });
    }

    class UserAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private List<UserBean> mList = new ArrayList<>();
        private Context mContext;

        public void setData(List<UserBean> list) {
            this.mList = list;
            notifyDataSetChanged();
        }

        public void updateData(String id, String onlineStatus) {
            for (UserBean userBean : mList) {
                if (!TextUtils.isEmpty(id) && id.equals(userBean.getId())) {
                    userBean.setOnline(onlineStatus);
                }
            }
            notifyDataSetChanged();
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            mContext = parent.getContext();
            return new BodyViewHolder(LayoutInflater.from(mContext).inflate(
                    R.layout.item_user, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            final UserBean userBean = mList.get(position);
//            final FamilyInfo familyInfo = userBean.getFamilyInfo();
            final BodyViewHolder body = (BodyViewHolder) holder;
            String name = userBean.getName();
            String account = userBean.getAccount();
            Glide.with(mContext).load(userBean.getHeadPicUrl()).into(body.mIvAvatar);
            String onlineStatus = "在线";
            if (userBean.isOnline()) {
                onlineStatus = "在线";
                body.mTvOnlineStatus.setBackground(mContext.getResources().getDrawable(R.drawable.shape_cerulean));
                body.mTvOnlineStatus.setText(onlineStatus);
            } else {
                onlineStatus = "离线";
                body.mTvOnlineStatus.setBackground(mContext.getResources().getDrawable(R.drawable.shape_aluminium));
                body.mTvOnlineStatus.setText(onlineStatus);
            }
            body.mTvName.setText(name);
            body.mLlRoot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "ROOT view click position = " + position);
                    if(null != mOnItemClickListener){
                        mOnItemClickListener.onItemClick(userBean.getId());
                    }
//                    mActivity.onClickUserSelectItem(userBean.getId());
                }
            });
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        class BodyViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.checkbox)
            CheckBox mCheckbox;
            @BindView(R.id.iv_avatar)
            ImageView mIvAvatar;
            @BindView(R.id.tv_name)
            TextView mTvName;
            @BindView(R.id.ll_root)
            LinearLayout mLlRoot;
            @BindView(R.id.tv_online_status)
            TextView mTvOnlineStatus;

            public BodyViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }

    public interface OnItemClickListener {
        void onItemClick(String userId);
    }


}
